//
//  EFQuickPaySelectViewController.m
//  EnjogFood
//
//  Created by guocheng on 2017/5/18.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFQuickPaySelectViewController.h"
#import "EFPaySuccessViewController.h"
#import "EFSepecialItem.h"
#import "EFTransHomeDetailViewController.h"

@interface EFQuickPaySelectViewController ()

@property(strong,nonatomic)UILabel* tipsLabel;

@property(strong,nonatomic)UIImageView* selfBringIV;
@property(strong,nonatomic)UIImageView* transHomeIV;

@property (strong, nonatomic) NSMutableArray *data;

@end

@implementation EFQuickPaySelectViewController

- (instancetype)initWithShopData:(NSMutableArray *)shopData{
    self = [super init];
    if (self) {
        self.title = @"服务方式";
        self.data = shopData;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)initView{
    [self.view addSubview:self.tipsLabel];
    [self.view addSubview:self.selfBringIV];
    [self.view addSubview:self.transHomeIV];
}

-(void)payWayTapAction:(UITapGestureRecognizer*)sender{
   
    __weak typeof(self) weakSelf = self;
    if(sender.view == self.selfBringIV){
        [ICNavigator openLoginWithCompleteBlock:^{
            //cartUpdate
            //cast to id qty
            [weakSelf updateCart];
            EFTransHomeDetailViewController *vc = [[EFTransHomeDetailViewController alloc] initWithData:self.data type:0];
            [ICNavigator pushViewController:vc animated:YES];
//            EFPaySuccessViewController *paySuccess = [[EFPaySuccessViewController alloc] init];
//            [ICNavigator pushViewController:paySuccess animated:YES];

        }];
    }else if(sender.view == self.transHomeIV){
        [ICNavigator openLoginWithCompleteBlock:^{
            [weakSelf updateCart];
            EFTransHomeDetailViewController *vc = [[EFTransHomeDetailViewController alloc] initWithData:self.data type:1];
            [ICNavigator pushViewController:vc animated:YES];
        }];
    }
}

- (void)updateCart{
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
    for (EFSepecialItem *item in APP_CONTEXT.shoppingCartManager.scanProductsCart) {
        NSDictionary *dic = [NSDictionary dictionary];
        dic = @{
                @"id":item.id,
                @"qty":item.qty,
                @"barcode":item.barcode != nil ? item.barcode : @""
                };
        [array addObject:dic];
    }
    [APP_CONTEXT.net updateCartList:array total:[NSNumber numberWithInteger:array.count] type:[NSNumber numberWithInt:1] OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
        if (code == 0) {
            
        }
    }];
}


-(UILabel *)tipsLabel{
    if(_tipsLabel == NULL){
        _tipsLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 17)];
        _tipsLabel.font = [UIFont systemFontOfSize:15];
        _tipsLabel.textColor = kBlack999;
        [_tipsLabel setText:@"请选择服务方式"];
        [_tipsLabel sizeToFit];
        _tipsLabel.left = (kScreenWidth - _tipsLabel.width)/2;
        _tipsLabel.top = 107;
    }
    return _tipsLabel;
}

-(UIImageView *)selfBringIV{
    if(_selfBringIV == NULL){
        _selfBringIV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 120, 120)];
        [_selfBringIV setImage:IMG(@"takeaway_button_default")];
        _selfBringIV.left = (kScreenWidth - _selfBringIV.width)/2;
        _selfBringIV.top = self.tipsLabel.bottom + 30;
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(payWayTapAction:)];
        [self.selfBringIV addGestureRecognizer:tapGesture];
        self.selfBringIV.userInteractionEnabled = YES;
    }
    return _selfBringIV;
}


-(UIImageView *)transHomeIV{
    if(_transHomeIV == NULL){
        _transHomeIV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 120, 120)];
        [_transHomeIV setImage:IMG(@"distribution_button_default")];
        _transHomeIV.left = (kScreenWidth - _transHomeIV.width)/2;
        _transHomeIV.top = self.selfBringIV.bottom + 15;
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(payWayTapAction:)];
        [self.transHomeIV addGestureRecognizer:tapGesture];
        self.transHomeIV.userInteractionEnabled = YES;
    }
    return _transHomeIV;
}


@end
